import CryptoJS from "crypto-js";

/**
 * AES 加密
 * KEY:  需要前后端保持一致
 * mode: ECB  需要前后端保持一致
 * pad: Pkcs7 //前端 Pkcs7 对应 后端 Pkcs5
 */
const KEY = CryptoJS.enc.Utf8.parse("a761009ce3547b727e9387f2e6afadb2"); // 秘钥
// const KEY = CryptoJS.lib.WordArray.random(16).toString(CryptoJS.enc.Hex)

// 生成随机的iv，长度为16字节
// const iv = CryptoJS.lib.WordArray.random(16);

export const AES_Encrypt = plaintext => {
  let ciphertext = CryptoJS.AES.encrypt(plaintext, KEY, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }).toString();
  return ciphertext;
};

/**
 * AES 解密
 */
export const AES_Decrypt = jsonStr => {
  let plaintext = CryptoJS.AES.decrypt(jsonStr, KEY, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }).toString(CryptoJS.enc.Utf8);

  return plaintext;
};
